3616
23757
Portanto, tenho uma tabela que defini como uma entidade em hibernação assim:
@Entidade
@Table (name = "sec_Preference")
public class Preference {
id longo privado;
@Column (name = "PreferenceId", nullable = false, insertable = true, atualizável = true, length = 19, precisão = 0)
@GeneratedValue (strategy = GenerationType.AUTO)
@Eu iria
public long getId () {
return id;
}
public void setId (long id) {
this.id = id;
}
private long systemuserid;
@Column (name = "SystemUserId", nullable = true, insertable = true, atualizável = true, length = 19, precisão = 0)
@Basic
public long getSystemUserId () {
return systemuserid;
}
public void setSystemUserId (long systemuserid) {
this.systemuserid = systemuserid;
}
private long dbgroupid;
@Column (name = "DBGroupId", nullable = true, insertable = true, atualizável = true, length = 19, precision = 0)
@Basic
public long getDBGroupId () {
return dbgroupid;
}
public void setDBGroupId (long dbgroupid) {
this.dbgroupid = dbgroupid;
}
private long externalgroupid;
@Column (name = "ExternalGroupId", nullable = true, insertable = true, atualizável = true, length = 19, precision = 0)
@Basic
public long getExternalGroupId () {
return externalgroupid;
}
public void setExternalGroupId (long externalgroupid) {
this.externalgroupid = externalgroupid;
}
private long securityroleid;
@Column (name = "SecurityRoleId", nullable = true, insertable = true, atualizável = true, length = 19, precisão = 0)
@Basic
public long getSecurityRoleId () {
return securityroleid;
}
public void setSecurityRoleId (long securityroleid) {
this.securityroleid = securityroleid;
}
public void setEnum (com.vitalimages.common.server.security.Preference pref) {
this.preferencekey = pref.name ();
}
chave de preferência de string privada;
@Column (name = "PreferenceKey", nullable = false, insertable = true, atualizável = true, length = 255, precision = 0)
@Basic
public String getKey () {
retornar chave de preferência;
}
public void setKey (String key) {
this.preferencekey = chave;
}
private String preferencevalue;
@Column (name = "PreferenceValue", nullable = true, insertable = true, atualizável = true, length = 255, precisão = 0)
@Basic
public String getValue () {
return preferencevalue;
}
public void setValue (String value) {
this.preferencevalue = value;
}
}
Quando tentei escrever uma consulta simples nesta tabela:
public Collection  getPreferencesForDBGroup (long dbgroupId) {
critérios finais DetachedCriteria = DetachedCriteria.forClass (Preference.class)
.add (Restrictions.eq ("dbgroupid", dbgroupId))
.setResultTransformer (DistinctRootEntityResultTransformer.INSTANCE);
return getHibernateTemplate (). findByCriteria (critérios);
}
Recebi o seguinte erro:
org.springframework.orm.hibernate3.HibernateQueryException: não foi possível resolver a propriedade: dbgroupid of: com.common.server.domain.sec.Preference; a exceção aninhada é org.hibernate.QueryException: não foi possível resolver a propriedade: dbgroupid de: com.common.server.domain.sec.Preference
Por que o hibernate não consegue descobrir qual dbgroupid está na minha classe? 
Provavelmente é porque seu getter (e setter) não está seguindo a convenção JavaBeans. Deveria ser:
public long getDbgroupId () {
return dbgroupid;
}
O que eu sugiro é - nomeie seus campos e, em seguida, use seu IDE para gerar setters e getters. Isso seguirá a convenção. (Outra coisa, isso é uma questão de preferência, mas na minha opinião torna uma aula mais fácil de ler - anote seus campos, não getters)
|
Bem, eu fiz alguns progressos nisso, mas ainda não entendo de onde o Hibernate tira seus nomes. Eu depurei no estado de hibernação e encontrei a seguinte classe:
org.hibernate.persister.entity.AbstractPropertyMapping
Nesta aula, há um método:
public Type toType (String propertyName) throws QueryException {
Type type = (Type) typesByPropertyPath.get (propertyName);
if (type == null) {
lançar propertyException (propertyName);
}
tipo de retorno;
}
Que tenta resolver o nome dado nos critérios em relação ao objeto. Portanto, no mapa typesByPropertyPath, encontrei os seguintes valores:
id -> DBGroupId=org.hibernate.type.LongType@1e96ffd
chave -> valor=org.hibernate.type.StringType@aa2ee4
valor -> valor=org.hibernate.type.StringType@aa2ee4
systemUserId -> DBGroupId=org.hibernate.type.LongType@1e96ffd
securityRoleId -> DBGroupId=org.hibernate.type.LongType@1e96ffd
externalGroupId -> DBGroupId=org.hibernate.type.LongType@1e96ffd
DBGroupId -> DBGroupId=org.hibernate.type.LongType@1e96ffd
Aqui você pode ver que a CAPITALIZAÇÃO de DBGroupId não correspondeu ao que eu tinha em meus critérios. Então eu mudei de dbgroupid para DBGroupId assim:
public Collection  getPreferencesForDBGroup (long dbgroupId) {
critérios finais DetachedCriteria = DetachedCriteria.forClass (Preference.class)
.add (Restrictions.eq ("DBGroupId", dbgroupId))
.setResultTransformer (DistinctRootEntityResultTransformer.INSTANCE);
return getHibernateTemplate (). findByCriteria (critérios);
}
Agora funciona.
|
Talvez porque você o rotulou como "DBGroupId" e não como "dbgroupid"?
|
sua resposta
StackExchange.ifUsing ("editor", function () {
StackExchange.using ("externalEditor", function () {
StackExchange.using ("snippets", function () {
StackExchange.snippets.init ();
});
});
}, "partes de codigo");
StackExchange.ready (function () {
var channelOptions = {
tags: "" .split (""),
id: "1"
};
initTagRenderer ("". split (""), "" .split (""), channelOptions);
StackExchange.using ("externalEditor", function () {
// Tem que disparar o editor após os snippets, se os snippets estiverem habilitados
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using ("snippets", function () {
createEditor ();
});
}
outro {
createEditor ();
}
});
function createEditor () {
StackExchange.prepareEditor ({
useStacksEditor: false,
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputaçãoToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by \ u003ca href = \" https: //imgur.com/ \ "\ u003e \ u003csvg class = \" svg-icon \ "width = \" 50 \ "height = \" 18 \ "viewBox = \ "0 0 50 18 \" fill = \ "none \" xmlns = \ "http: //www.w3.org/2000/svg \" \ u003e \ u003cpath d = \ "M46.1709 9.17788C46.1709 8.26454 46,2665 7,94324 47,1084 7,58816C47.4091 7,46349 47,7169 7,36433 48,0099 7,26993C48.9099 6,97997 49,672 6,73443 49,672 5,93063C49,672 5,22043 48,9832 4,61182 48,1414 4,61182C47,4335 4,26993C48.9099 6,97997 49,672 6,73443 49,672 5,93063C49,672 5,22043 48,9832 4,61182 48,1414 4,61182C47,4335 4,6431182 4,25.654,23,7623,74,6281 4,9281 467823,621 4,96281 4,9823,623,623,461,45,023,74,6281 4,9823,1623,623,14,6281 4,9282 C 4,9281 4,923,623,623,76,2 53,76,23,76,823,14,65,0281 4,928,023,623,14,65,0281 4,928,023,623. 43.1481 6.59048V11.9512C43.1481 13.2535 43.6264 13.8962 44.6595 13.8962C45.6924 13.8962 46.1709 13.2535 46.1709 11.9512V9.17788Z \ "/ \ u003e \ u003cpath d = \" M32.492 10.1439219 C32.492.492. 41.5985 12.6954 41.5985 10.1419V6.59049C41.5985 5.28821 41.1394 4.66232 40.1061 4.66232C39.0732 4.66232 38.5948 5.28821 38.5948 6.59049V9.60062C38.5948 10.8521 38.2696 11.5455 37.0454 11.5204 11.5204 11.5458.5948.5948 5.28821 38.5948 6.59049V9.60062C38.5948 10.8521 38.2696 11.5455 37.0454 11.520 11.520 11.5204 11.520 11.5204 11.5204 11.5458 11.5204 11.5204 11.5204 11.520 c 11.5204 11.520. 521 35.4954 9.60062V6.59049C35.4954 5.28821 35.0173 4.66232 34.0034 4.66232C32.9703 4.66232 32.492 5.28821 32.492 6.59049V10.1419Z \ "/ \ u003e \ u003cpath fill-rule = \" evenodd \ "clip-rule = \" evenodd \ "clip-rule = ” .1369 4.56087 21.0134 6.57349 21.0134 9.27932C21.0134 11.9852 23.003 13,913 25.3754 13.913C26.5612 13,913 27.4607 13.4902 28.1109 12.6616C28.1109 12.7229 28.1161 12.7799 28.7799 28.772.1 12.8346C 25.3754 13.913C26.5612 13,913 27.4607 13.4902 28.1109 12.6616C28.1109 12.7229 28.1161 12.7799 28.7799 2877121 12.8346C 25.3754 13.913C26.5612 13,913 27.4607 13.4902 28.1109 12.6616C28.1109 12.7229 28,1161 12.7799 287799 2877323 12.8346C 28.1256.1 12.34.273 12.34.273.228.125.303 12.34.273 12.34.273. 15.2321 24.1352 14.9821 23.5661 14.7787C23.176 14.6393 22.8472 14.5218 22.5437 14.5218C21.7977 14.5218 21.2429 15.0123 21.2429 15.6887C21.2429 16.7375 22.9072 17.6335 25.6622 17.6335ZM.13.13.17.24.24.24.24.27.6335ZM.17.23.17.24.24.24.24.24.24.117.9072 17.6335 25.6622 17.632,10 7,01724.1317 9,24.24.24,10 267.072 7,01724.1317 9,24.24.24,10 269.072 7,01724.1317 9,24.24.24,10 269.072 7,01724.1317 9,24.24.24,10 269 7,01724.17 9,24,24,24,10 269 7,01724,17 9,24,24,10,29.7.09766C27. .3298 13,8962 19,8079 13,2535 19,8079 11,9512V8.12928C19,8079 5,82936 18,4879 4,62866 16,4027 4,62866C15,1594 4,62866 14,279 4,98375 13,3609 5,88013C12,653 5,05154 11,6581 4,62866 10,3573 4,62866C .47873 4.66232 5.00066 5.28821 5.00066 6.59049V11.9512C5.00066 13.2535 5.47873 13.8962 6.51203 13.8962C7.54479 13.8962 8.0232 13.2535 8.0232 11.9512V8.90741C8.0232 7.58817 8.44431 6.91179 9.5345.10.59. 13,8962 12,4044 13,8962C13,4375 13,8962 13,9157 13,2535 13,9157 11,9512V8.90741C13,9157 7,58817 14,3365 6,91179 15,4269 6,91179C16.4027 6,91179 16,8045 7,58817 16,8045 8,94108V11,9512Z \ ' / \ u003e \ u003cpath d = \ `` M3.31675 6.59049C3.31675 5.28821 2.83866 4.66232 1.82471 4.66232C0.791758 4.66232 0,313354 5.28821 0,313354 \ "/ \ u003e \ u003cpath d = \" M1.87209 0,400291C0,843612 0,400291 0 1,1159 0 1,98861C0 2,87869 0,822846 3,57676 1,87209 3,57676C2,90056 3,57676 3,7234 2,87869 3,7234 1,98861C3,7234 1,19159 0,49 \ "# 1BB76E \" / \ u003e \ u003c / svg \ u003e \ u003c / a \ u003e ",
contentPolicyHtml: "Contribuições do usuário licenciadas sob \ u003ca href = \" https: //stackoverflow.com/help/licensing \ "\ u003ecc by-sa \ u003c / a \ u003e \ u003ca href = \" https://stackoverflow.com / legal / content-policy \ "\ u003e (política de conteúdo) \ u003c / a \ u003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
, imediatamenteShowMarkdownHelp: true, enableTables: true, enableSnippets: true
});
}
});
Obrigado por contribuir com uma resposta para Stack Overflow!
Certifique-se de responder à pergunta. Forneça detalhes e compartilhe sua pesquisa!
Mas evite ...
Pedir ajuda, esclarecimento ou responder a outras respostas.
Fazer declarações com base em opinião; Apoie-os com referências ou experiência pessoal.
Para saber mais, veja nossas dicas sobre como escrever boas respostas.
Rascunho salvo
Rascunho descartado
Cadastre-se ou faça o login
StackExchange.ready (function () {
StackExchange.helpers.onClickDraftSave ('# login-link');
});
Inscreva-se usando o Google
Cadastre-se usando o Facebook
Inscreva-se usando e-mail e senha
Enviar
Postar como convidado
Nome
O email
Obrigatório, mas nunca mostrado
StackExchange.ready (
function () {
StackExchange.openid.initPostLogin ('. New-post-login', 'https% 3a% 2f% 2fstackoverflow.com% 2fquestions% 2f4441837% 2fhibernate-throws-hibernatequeryexception-could-not-resolve-property% 23new-answer', ' question_page ');
}
);
Postar como convidado
Nome
O email
Obrigatório, mas nunca mostrado
Publique a sua resposta
Descartar
Ao clicar em “Publique sua resposta”, você concorda com nossos termos de serviço, política de privacidade e política de cookies
Não é a resposta que você está procurando? Navegue por outras questões com a tag java hibernate spring ou faça sua própria pergunta.